package com.financial.business.integral.service.yd.impl;

import com.financial.business.integral.config.UrlConfig;
import com.financial.business.integral.constants.FinancialIntegralEnumConstants;
import com.financial.business.integral.dto.FinancialYDIntegralPayQueryDTO;
import com.financial.business.integral.service.yd.FinancialYDIntegralPayQueryService;
import com.financial.business.integral.utils.HttpClientUtils;
import com.financial.business.integral.utils.JsonUtils;
import com.financial.business.integral.utils.ValidateUtils;
import com.financial.business.integral.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //优店积分查询
 * @Date 2019/12/5 16:50
 */
@Service
public class FinancialYDIntegralPayQueryServiceImpl implements FinancialYDIntegralPayQueryService {
    public static final Logger logger = LoggerFactory.getLogger(FinancialYDIntegralPayQueryServiceImpl.class);

    @Autowired
    private HttpClientUtils httpClientUtils;

    @Override
    public ApiResult payQuery(String requestParam) {
        logger.info("FinancialYDIntegralPayQueryServiceImpl.payQuery---接收够样余额支付查询参数json" + requestParam);
        FinancialYDIntegralPayQueryDTO financialYDIntegralPayQueryDTO = JsonUtils.convertBean(requestParam, FinancialYDIntegralPayQueryDTO.class);
        Map<String, Object> params = JsonUtils.jsonToMap(requestParam);
        /*三方支付专用的交易单号*/
        Object tempParam = params.get("out_trade_no");
        String outTradeNo = tempParam == null ? null : tempParam.toString();
        /*业务类型*/
        tempParam = params.get("business_type");
        String businessType = tempParam == null ? null : tempParam.toString();
        /*现在支付渠道类型编码*/
        tempParam = params.get("pay_channel_no");
        String payChannelNo = tempParam == null ? null : tempParam.toString();
        /*对接平台编码*/
        tempParam = params.get("platform_no");
        String platformNo = tempParam == null ? null : tempParam.toString();
        if (StringUtils.isBlank(outTradeNo) || StringUtils.isBlank(businessType)
                || StringUtils.isBlank(platformNo) || StringUtils.isBlank(payChannelNo)) {
            /*对所传字段进行非空校验判断*/
            return ApiResult.failure(ValidateUtils.validateProperty(financialYDIntegralPayQueryDTO));
        }
        String result = "";
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("orderNum", outTradeNo);
        try {
            result = httpClientUtils.doPost(UrlConfig.INTEGRAL_YD_PAY_QUERY_URL, reqMap);
            /*判断优店积分查询回执*/
            if (!"".equals(result)) {
                /*解析返回信息判断状态*/
                Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
                /*1---成功标识*/
                Boolean state = resultMap.containsKey("state");
                if (state && (resultMap.get("state").toString()).equalsIgnoreCase(FinancialIntegralEnumConstants.ENUM_YDINTEGRALPAY_SUC)) {
                    /*返回成功信息*/
                    logger.info("FinancialYDIntegralPayQueryServiceImpl.payQuery---调用优店积分主动查询返回信息" + result);
                    return ApiResult.success(result);
                }
            } else {
                logger.info("FinancialYDIntegralPayQueryServiceImpl.payQuery---调用优店积分主动查询返回信息" + result);
                String resultExe = "请输入正确的单号信息";
                return ApiResult.failure(resultExe);
            }
            logger.info("FinancialYDIntegralPayQueryServiceImpl.payQuery---调用优店积分主动查询返回信息" + result);
            return ApiResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialYDIntegralPayQueryServiceImpl.payQuery---查询积分预支付信息失败!" + e.getMessage());
        }
        return ApiResult.failure();
    }
}
